const _ = require('lodash');
const moment = require('moment');
const prefix = '/api/spider-types';

module.exports = (app) => {
  const {router, middleware, spider} = app;

  //根据开始日期和结束，检索每日分类的数据
  router.get(prefix + '/data/:type_id/:start/:end', middleware.api(2),
      async ctx => {
        let {type_id, start, end} = ctx.params;
        let platforms = await spider.Platform.findAll(
            {where: {disabled: false}});
        platforms = _.keyBy(platforms, 'id');
        const res = await spider.TypeData.findAll({
          order: [['created_at', 'asc'], ['platform_id', 'asc']],
          where: {
            created_at: {[ctx.app.Sequelize.Op.between]: [start, end]},
            type_id,
          },
        });
        const final = {table: {}, chart: {}};
        start = moment(start);
        end = moment(end);

        //按 日期 组合
        const dateData = _.groupBy(res, 'created_at');
        const legend = {data: _.map(platforms, 'name')};
        //日期
        const date = [];
        //5个数据项目
        const num_watch = {}, num_new = {}, num_open = {}, num_active = {};
        //按平台初始化5个数据项目
        _.each(platforms, p => {
          num_watch[p.id] = {
            name: p.name,
            type: 'line',
            data: [],
          };
          num_new[p.id] = {
            name: p.name,
            type: 'line',
            data: [],
          };
          num_open[p.id] = {
            name: p.name,
            type: 'line',
            data: [],
          };
          num_active[p.id] = {
            name: p.name,
            type: 'line',
            data: [],
          };
        });

        let tableData;

        //数据表 逻辑 区别，以日期为y轴，平台为x轴
        {
          tableData = [];
          let diff = end.diff(start, 'day');
          while (diff >= 0) {
            const created_at = start.format('YYYY-MM-DD');
            const dateString = start.format('MM月DD日');
            let row = {
              date: dateString,
              num_watch: {},
              num_new: {},
              num_active: {},
              num_open: {},
            };

            tableData.push(row);
            const data = dateData[created_at];
            if (data) {
              // console.log(created_at, '有数据');
              _.each(platforms, p => {
                const id = p.id;
                let d = _.find(data, {platform_id: id});
                let watchData = 0, _newData = 0, openData = 0, activeData = 0;
                if (d) {
                  watchData = d.num_watch;
                  _newData = d.num_new;
                  openData = d.num_open;
                  activeData = d.num_active;
                }
                num_watch[id].data.push(watchData);
                num_new[id].data.push(_newData);
                num_open[id].data.push(openData);
                num_active[id].data.push(activeData);
                row.num_watch[id] = watchData;
                row.num_new[id] = _newData;
                row.num_open[id] = openData;
                row.num_active[id] = activeData;
              });
            } else {
              _.each(platforms, p => {
                const id = p.id;
                num_watch[id].data.push(0);
                num_new[id].data.push(0);
                num_open[id].data.push(0);
                num_active[id].data.push(0);
                row.num_watch[id] = 0;
                row.num_new[id] = 0;
                row.num_open[id] = 0;
                row.num_active[id] = 0;
              });
            }

            date.push(dateString);
            start.add(1, 'day');
            diff = end.diff(start, 'day');
          }
          final.table.data = tableData;
          final.table.date = date;
        }

        final.chart.num_watch = Object.values(num_watch);
        final.chart.num_open = Object.values(num_open);
        final.chart.num_new = Object.values(num_new);
        final.chart.num_active = Object.values(num_active);
        final.chart.legend = legend;
        final.chart.xAxis = {data: date};
        ctx.body = final;
      });
};